package unittests;
import java.util.ArrayList;

import junit.framework.TestCase;
import model.CustomerPhotosModel;
import model.SearchPhotoModel;
import model.PermissionModel;
import controllers.SearchPhotoController;
import controllers.SearchResultController;
import controllers.ViewPhotoSampleController;

/**
 * The Class ViewSampleTest.
 * Checks functions that deals with viewSample.
 * @author Tal
 */

public class ViewSampleTest extends TestCase 
{
	
	/** The user per. */
	ArrayList<PermissionModel> userPer=null;
	
	/** The photos. */
	ArrayList<SearchPhotoModel> photos=null;
	
	/** The cus photos. */
	ArrayList<CustomerPhotosModel> cusPhotos=null;
		
	
	/**
	 * Instantiates a new view sample test.
	 *
	 * @param Name the name
	 */
	public ViewSampleTest(String Name)
	{
		super(Name);
	}
	
	
	protected void setUp() throws Exception
	{
		//create users
		userPer = new ArrayList<PermissionModel>(); 
		
		//Registered Customer-uid-1	
		userPer.add(new PermissionModel(1,1,"Registered Customer"));
		
		//Registered Customer-uid-2
		userPer.add(new PermissionModel(2,1,"Registered Customer"));
		       //add package to uid 2
		       userPer.add(new PermissionModel(2,2,"Package Customer"));
		
		//Registered Customer-uid-3
		userPer.add(new PermissionModel(3,1,"Registered Customer"));
		        //add Daily to uid 3
		         userPer.add(new PermissionModel(3,5,"Daily Photo"));
		         
		 //Archive Worker-uid-4
		  userPer.add(new PermissionModel(4,4,"Archive Worker"));
		  
		//Registered Customer-uid-5
			userPer.add(new PermissionModel(5,1,"Registered Customer"));
			        //add Business Layer to uid 5
			         userPer.add(new PermissionModel(5,6,"Business Layer"));
			       //add Infrastructure Layer to uid 5
			         userPer.add(new PermissionModel(5,7,"Infrastructure Layer"));	
			         
			         
      //create photos
	    photos=	new ArrayList<SearchPhotoModel>();
	    photos.add(new SearchPhotoModel("12:00",1,"c:\\pic1"));
	    photos.add(new SearchPhotoModel("17:00",1,"c:\\pic2"));
	    photos.add(new SearchPhotoModel("08:00",2,"c:\\pic3"));
	    photos.add(new SearchPhotoModel("12:00",2,"c:\\pic4"));
	    photos.add(new SearchPhotoModel("20:00",3,"c:\\pic5"));
	    photos.add(new SearchPhotoModel("10:00",3,"c:\\pic6"));
	  
	    
	   //create user watch list
		 cusPhotos=new ArrayList<CustomerPhotosModel>();
	     cusPhotos.add(new CustomerPhotosModel(1,photos.get(0)));
	     cusPhotos.add(new CustomerPhotosModel(1,photos.get(2)));
	     cusPhotos.add(new CustomerPhotosModel(5,photos.get(5)));
	     cusPhotos.add(new CustomerPhotosModel(5,photos.get(0)));
	     
        
	}
	
	
	/**
	 * Test photo.
	 * Photos object define the Photos that saved in DB
	 * Checks if photo1,photo2 exist in photos object
	 */
	public void testPhoto()
	{ 
		//photo does not exist
		SearchPhotoModel photo1=new SearchPhotoModel("15:00",2,"c:\\pic6");
	    SearchPhotoController sPhotoController=new SearchPhotoController(photo1);
	    sPhotoController.SetPhotos(photos);
		assertFalse(sPhotoController.checkPhotoExist());
	
		//photo exist
         SearchPhotoModel photo2=new SearchPhotoModel("12:00",1,"c:\\pic1");	
	     SearchPhotoController sPhotoController2=new SearchPhotoController(photo2);
	     sPhotoController2.SetPhotos(photos);
	 	 assertTrue(sPhotoController2.checkPhotoExist());
		
	}
	
	
	/**
	 * Test permission.
	 * userPer object define the userPermission for all users in DB
	 * checks if user is RegisterdCustomer  only him can buy/View Sample. 
	 */
	public void  testPermission()
	{
		//RegisterdCustomer
	     SearchPhotoController search=new SearchPhotoController();
	     search.setUserPermissions(userPer);
	     
	     ArrayList<PermissionModel> u1=new ArrayList<PermissionModel>();
	   //Registered Customer-uid-3
	     u1.add(new PermissionModel(3,1,"Registered Customer"));
			    //add Daily to uid 3
	     u1.add(new PermissionModel(3,5,"Daily Photo"));
		 
	     assertFalse(search.checkPermission(u1));
	     
	     //archiveWorker
	     SearchPhotoController search2=new SearchPhotoController();
	     search2.setUserPermissions(userPer);
	     
	     ArrayList<PermissionModel> u2=new ArrayList<PermissionModel>();
	     //Archive Worker-uid-4
	     u2.add(new PermissionModel(4,4,"Archive Worker"));
		  
	     assertTrue(search2.checkPermission(u2));
	}
	
	/**
	 * Test watch.
	 * CustomerPhotos object define the Photos that customer watch and saved in DB
	 * Checks if photo1,photo2 exist in CustomerPhotos object
	 */
	public void  testWatch()
	{
		//not watch
		 SearchResultController res=new SearchResultController();
		 res.setWatchPhotos(cusPhotos);
		 SearchPhotoModel photo1=new SearchPhotoModel("20:00",3,"c:\\pic5");
		 CustomerPhotosModel cusPhoto=new CustomerPhotosModel(2,photo1);
		
	     assertFalse(res.watchPhoto(cusPhoto));
	    
	     
	     //watch
		 SearchResultController res2=new SearchResultController();
		 res2.setWatchPhotos(cusPhotos);
		 SearchPhotoModel photo2=new SearchPhotoModel("12:00",1,"c:\\pic1");
		 CustomerPhotosModel cusPhoto2=new CustomerPhotosModel(5,photo2);
	     assertTrue(res2.watchPhoto(cusPhoto2));
	     
	}
	
	
	
	/**
	 * Test package.
	 * userPer object define the userPermission for all users in DB
	 * checks if user Has PackegeCustomer to charge/not charge  Sample. 
	 */
	public void testPackage()
	{
		//create search result with user 2-has package
		 SearchResultController result=new SearchResultController();
		 result.setUserPermissions(userPer);
		 
		 ArrayList<PermissionModel> user1=new ArrayList<PermissionModel>();
		//Registered Customer-uid-2
		 user1.add(new PermissionModel(2,1,"Registered Customer"));
		  //add package to uid 2
		 user1.add(new PermissionModel(2,2,"Package Customer"));
		 
		 assertTrue(result.hasPackege(user1));

			 
		//create search result with user 5-doesnt has package
		 SearchResultController result2=new SearchResultController();
		 result2.setUserPermissions(userPer);
		 
		 ArrayList<PermissionModel> user2=new ArrayList<PermissionModel>();
			//Registered Customer-uid-5
			 user2.add(new PermissionModel(5,1,"Registered Customer"));
				        //add Business Layer to uid 5
			 user2.add(new PermissionModel(5,6,"Business Layer"));
				       //add Infrastructure Layer to uid 5
			 user2.add(new PermissionModel(5,7,"Infrastructure Layer"));
			 
		 assertFalse( result2.hasPackege(user2));
		 
		 
	}
	
	/**
	 * Test View.
	 * photos object define the Photos that saved in DB
	 * Checks if sample1,sample2 can view or file corrupted
	 */
	public void  testView()
	{
		//problem with the file 
		
		 SearchPhotoModel sample1=new SearchPhotoModel("21:00",8,"c:\\pic8");
		ViewPhotoSampleController view=new ViewPhotoSampleController(sample1);
		view.SetPhotos(photos);
		
	     assertFalse(view.handleSampleSuccess());
	    
	     
	     //Success -View Sample
	     SearchPhotoModel sample2=new SearchPhotoModel("17:00",1,"c:\\pic2");
	     ViewPhotoSampleController view2=new ViewPhotoSampleController(sample2);
	     view2.SetPhotos(photos);
		 
	     assertTrue(view2.handleSampleSuccess());
	     
	}
	
	
}
